package com.bluefish.bltuan.module.shop;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

import com.bluefish.bltuan.module.user.User;
import com.bluefish.bltuan.tools.shop.TicketState;

/**
 * 消费卷
 * @author Blue
 *
 */
@Entity
public class Ticket implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;
	
	@OneToOne(cascade=CascadeType.REFRESH,fetch = FetchType.LAZY)  
	private TuanGou tuanGou;

	@OneToOne(cascade=CascadeType.REFRESH,fetch = FetchType.LAZY)  
	private Order order;
	
	@OneToOne(cascade=CascadeType.REFRESH,fetch = FetchType.LAZY)  
	private User user;

	private TicketState state;
	
	private int password;
	
	public int getPassword() {
		return password;
	}

	public void setPassword(int password) {
		this.password = password;
	}

	public TicketState getState() {
		return state;
	}

	public void setState(TicketState state) {
		this.state = state;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public TuanGou getTuanGou() {
		return tuanGou;
	}

	public void setTuanGou(TuanGou tuanGou) {
		this.tuanGou = tuanGou;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}
	
	public boolean isOutTime(){
		return false;
	}

	public void initByOrder(Order order){
		this.setOrder(order);
		this.setTuanGou(order.getTuangou());
		this.setUser(order.getUser());
		this.setState(TicketState.UNUSE);
		this.setPassword(10000+(int)(Math.random()*89999));
	}
	
	public String getIndexNum(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(getId());
		buffer.append(getTuanGou().getId());
		buffer.append(getOrder().getId());
		buffer.append(getUser().getId());
		return buffer.toString();
	}
}
